// 数据合并
export function mergeRecursive (source, target) {
  for (var p in target) {
    try {
      if (target[p].constructor == Object) {
        source[p] = mergeRecursive(source[p], target[p]);
      } else {
        source[p] = target[p];
      }
    } catch (e) {
      source[p] = target[p];
    }
  }
  return source;
}

/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
export function handleTree (data, id, parentId, children) {
  let config = {
    id: id || 'id',
    parentId: parentId || 'parentId',
    childrenList: children || 'children'
  };

  var childrenListMap = {}
  var nodeIds = {}
  var tree = []

  for (let d of data) {
    let parentId = d[config.parentId]
    if (childrenListMap[parentId] == null) {
      childrenListMap[parentId] = []
    }
    nodeIds[d[config.id]] = d
    childrenListMap[parentId].push(d)
  }

  for (let d of data) {
    let parentId = d[config.parentId]
    if (nodeIds[parentId] == null) {
      tree.push(d)
    }
  }

  for (let t of tree) {
    adaptToChildrenList(t)
  }

  function adaptToChildrenList (o) {
    if (childrenListMap[o[config.id]] !== null) {
      o[config.childrenList] = childrenListMap[o[config.id]]
    }
    if (o[config.childrenList]) {
      for (let c of o[config.childrenList]) {
        adaptToChildrenList(c)
      }
    }
  }
  return tree
}
/**
 * 文件转base64字符串
 * @param {*} file 
 */
export const fileToBase64Str = (file) => {
  return new Promise((resolve, reject) => {
    try {
      const fileReader = new FileReader()

      fileReader.readAsDataURL(file)

      fileReader.onload = (e) => {
        resolve(e.target.result)
      }
    } catch (error) {
      reject(error)
    }
  })
}
/**
 * 生成缩略图
 * @param {*} file 
 * @returns 
 */
export const handleGenerateThumbnail = (file, isFile = true, size = 500) => {
  return new Promise((resolve, reject) => {
    try {
      const canvas = document.createElement('canvas')

      const ctx = canvas.getContext('2d')

      const fileReader = new FileReader()

      const image = new Image()

      fileReader.readAsDataURL(file)

      fileReader.onload = (e) => {

        image.src = e.target.result

        image.onload = () => {

          const width = image.width

          const height = image.height

          const max = Math.max(width, height)

          const scale = max / size

          canvas.width = width / scale

          canvas.height = height / scale

          ctx.drawImage(image, 0, 0, canvas.width, canvas.height)

          if (isFile) {
            canvas.toBlob(blob => {
              const newFile = new File([blob], file.name, { type: file.type })
              resolve(newFile)
            })
          }
          else {
            canvas.toBlob(blob => {
              resolve(window.URL.createObjectURL(blob))
            })
          }
        }
      }
    } catch (error) {
      console.log(error)
      reject(error)
    }
  })
}
/** 转换值为字符串类型 */
export const handleConvertToString = (value) => {
  let result = undefined

  const _value = `${value}`

  if (typeof (value) === "number" && _value) {
    result = _value
  } else {
    result = value
  }

  return result
}